
import java.util.concurrent.atomic.AtomicInteger;

public class MyThread extends Thread{
	
	int id;
	static AtomicInteger nr_threads_atomic;
	static int nr_threads = 0;

	final static Object lock1 = new Object();
	final static Object lock2 = new Object();
	
	public MyThread(int id) {
		this.id = id;
	}

	  private void lock12() {
	       synchronized (lock1) {
	           try {
	        	   sleep(10);
			
		           synchronized (lock2) {
		               sleep(10);
		           }
	           } catch (InterruptedException e) {
					e.printStackTrace();
	           }
	       }
	   }
	 
	   private void lock21() {
	       synchronized (lock2) {
	          try {
	        	  sleep(10);
		           synchronized (lock1) {
		               sleep(10);
		           }
		       } catch (InterruptedException e) {
					e.printStackTrace();
		       }
	       }
	   }
	 
	public void run() {
		//System.out.println("eu sunt "+id);
		if(id ==0 ){
			lock12();
		} else {

			lock21();
		}
	}

	public static void main(String[] args) {
		int nr = 2; 
		MyThread[] threads = new MyThread[nr];
		for(int i =0; i< nr; i++) {
			threads[i] = new MyThread(i);
		}
		
		for(MyThread thread : threads) {
			thread.start();
		}
		
		for(MyThread thread : threads) {
			try {
				thread.join();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		System.out.println("finished");
	}

}
